#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
    string reverseOnlyLetters(string s) {
        if(!s.length())return s;
        int i = 0, j = s.length() - 1;
        while(i < j){
            for(; i < j; i++){if(isalpha(s[i]))break;}
            for(; i < j; j--){if(isalpha(s[j]))break;}
            swap(s[i++], s[j--]);
        }
        return s;
    }
};